﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SmartIMS.Mvc.Entities;
using SmartIMS.Core;
using SmartIMS.Core.Models;
using SmartIMS.Mvc.Models;
using log4net;

namespace SmartIMS.Mvc.Controllers
{
    [HandleError]
    public class ListtypeController : Controller
    {
        private static ILog _Loger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public ActionResult List(ListtypeFilter pListtypeFilter)
        {

            try
            {
                var objListtypeModel = new ListtypeModel();
                if (TempData["InitData"] != null)
                {
                    pListtypeFilter = (ListtypeFilter)TempData["InitData"];
                }
                var objListtypeList = objListtypeModel.GetAllListtype(pListtypeFilter.NameFilter, pListtypeFilter.Page, pListtypeFilter.NumerRecordPerPage);
                pListtypeFilter.List = objListtypeList;
                pListtypeFilter.TotalRecord = objListtypeModel.TotalRecord;
                return View(pListtypeFilter);
            }
            catch (Exception ex)
            {
                _Loger.Error("User",ex);
                return View("Error");
            }
        }

        public ActionResult Detail(ListtypeFilter pListtypeFilter, string type)
        {
            try
            {
                if (type == "Update")
                {
                    if (ModelState.IsValid)
                    {
                        var objListtypeModel = new ListtypeModel();
                        var bolResult = objListtypeModel.UpdateListtype(pListtypeFilter.Detail);
                        if (!bolResult)
                        {
                            ViewData["ErrorMessage"] = objListtypeModel.ErrorMessage;
                            return View(pListtypeFilter);
                        }
                        else
                        {
                            TempData["InitData"] = pListtypeFilter;
                            return RedirectToAction("List");
                        }
                    }
                    else
                    {
                        return View(pListtypeFilter);
                    }
                }
                else
                {
                    var objListtype = new Listtype();
                    var objListtypeModel = new ListtypeModel();
                    pListtypeFilter.Detail = objListtypeModel.GetSingleListtype(pListtypeFilter.ListtypeId);
                    return View(pListtypeFilter);
                }
            }
            catch (Exception ex)
            {
                _Loger.Error(ex);
                return View("Error");
            }
        }

        public ActionResult Delete(ListtypeFilter pListtypeFilter)
        {
            try
            {
                var objListtypeModel = new ListtypeModel();
                var bolResult = objListtypeModel.DeleteListtype(pListtypeFilter.ListSelected);
                if (!bolResult)
                {
                    ViewData["ErrorMessage"] = objListtypeModel.ErrorMessage;
                    return View(pListtypeFilter);
                }
                else
                {
                    TempData["InitData"] = pListtypeFilter;
                    return RedirectToAction("List");
                }
            }
            catch (Exception ex)
            {
                _Loger.Error(ex);
                return View("Error");
            }
        }
    }
}
